package demo.jdbc.example;

import demo.jdbc.example.dao.UsersDao;
import demo.jdbc.example.domain.User;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet(name = "ServletUpdateUserTest", value = "/ServletUpdateUserTest")
public class ServletUpdateUserTest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 修改User对象的数据
        UsersDao usersDao = new UsersDao();

        // id=8的USER更新
        User user = new User();
        user.setId(1);
        user.setUsername("xiaoliu");
        user.setPassword("456");
        user.setEmail("xiaoliu@sina.com");
        user.setBirthday(new Date());

        boolean flag = usersDao.update(user);

        PrintWriter out = response.getWriter();

        if (flag) {
            out.println("更新数据成功，2秒后跳转到所有用户数据列表");
            response.setHeader("refresh", "2;url=ServletFindAllUsersTest");
        } else {
            out.println("更新数据失败");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
